package com.gln.bloger.dao.impl;

import java.sql.SQLException;
import java.util.List;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Query;
import org.hibernate.Session;
import org.hibernate.criterion.DetachedCriteria;
import org.hibernate.criterion.Expression;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.gln.bloger.dao.CommentDao;
import com.gln.bloger.exception.BlogerException;
import com.gln.bloger.exception.DBOperateException;
import com.gln.bloger.pojos.Comment;
import com.gln.bloger.pojos.Entry;
import com.gln.bloger.pojos.Website;

public class CommentDaoImpl extends BaseDaoImpl<Comment> implements CommentDao{

	/**
	 * @param entry
	 * @throws BlogerException
	 * @return List<Comment>
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> getByEntry(Entry entry) throws BlogerException {
		try{
			DetachedCriteria criteria = DetachedCriteria.forClass(Comment.class);
			criteria.add(Restrictions.eq("entry", entry));
			return (List<Comment>) getHibernateTemplate().findByCriteria(criteria);
		}catch(HibernateException he){
			throw new DBOperateException(he.getMessage());
		}
	}

	/**
	 * get by author id
	 * @param author id
	 * @throws BlogerException
	 * @return List<Comment>
	 */
	@SuppressWarnings("unchecked")
	public List<Comment> getByAuthorId(String authorId) throws BlogerException {
		try{
			//make criteria
			Session session = getSession();
			Criteria criteria = session.createCriteria(Comment.class);
			criteria.add(Expression.conjunction().
					             add(Expression.eq("authorId", authorId)));
			return (List<Comment>) criteria.list();
		}catch(HibernateException he){
			throw new DBOperateException(he.getMessage());
		}
	}

	@SuppressWarnings("unchecked")
	public List<Comment> getAllComment(final Website website)
			throws BlogerException {

		List<Comment> comments = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("from Comment  comment where " +
						"comment.entry.website = :website order by comment.postTime asc");
				query.setParameter("website", website);
				query.setCacheable(true);
				return query.list();
			}});
			return comments;
	}

	@SuppressWarnings("unchecked")
	public List<Comment> getRecentComments(final Website website, final int num)
			throws BlogerException {
		List<Comment> comments = getHibernateTemplate().executeFind(new HibernateCallback(){
			public Object doInHibernate(Session session) throws HibernateException, SQLException {
				Query query = session.createQuery("from Comment  comment where " +
						"comment.entry.website = :website and comment.status=:status order by comment.postTime desc");
				query.setParameter("status", Comment.Status.APPROVED);
				query.setParameter("website", website);
				query.setMaxResults(num);
				query.setCacheable(true);
				return query.list();
			}});
			return comments;
	}


	public List<Comment> getWaitForApproveComments(Website website)
			throws BlogerException {
		return null;
	}

}
